package descentFly.app.managePanel
{
	import SEApp.se_util;
	
	import UIFree.controls.uif_gbutton;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import descentFly.app.df_baseApp;
	import descentFly.app.df_component;
	import descentFly.app.df_events;
	import descentFly.app.df_label;
	import descentFly.app.df_panel;
	import descentFly.df_optionsObject;
	import descentFly.df_player;
	import descentFly.df_space;
	import descentFly.weaponts.element.df_plasmaElement;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_playerInfo extends df_panel {
		private var _login	: df_label;
		private var _victory: df_paramItem;
		private var _lesions: df_paramItem;
		private var _bonus	: df_paramItem;
		private var _loginButton	: uif_gbutton;
		
		public function df_playerInfo(parent:uif_component, rect: Rectangle, a_styleClass:Object='playerInfo') {
			super(parent, rect.x, rect.y, rect.width, rect.height, a_styleClass);
		}
		
		override protected function createComponents(layer:df_component, inner:Rectangle):void {
			if (player) updatePlayer();
		}
		
		override protected function listenEvents():void {
			if (player) listenEventPlayer();
			dfApp.space.addEventListener(df_space.SETPLAYER, doSetPlayer);
			super.listenEvents();
		}
		
		protected function doSetPlayer(e: Event): void {
			listenEventPlayer();
		}
		
		override public function clearEvents():void {
			if (player) removeEventPlayer();
			dfApp.space.removeEventListener(df_space.SETPLAYER, doSetPlayer);
			super.clearEvents();
		}
		
		protected function listenEventPlayer(): void {
			if (player) removeEventPlayer();
			player.addEventListener(df_events.CHANGEPLAYERSTATUS, doPlayerUpdateValues);
			player.addEventListener(df_optionsObject.SETOPTIONS, doSetOptionsPlayer);
			
		}
		
		protected function removeEventPlayer(): void {
			player.removeEventListener(df_events.CHANGEPLAYERSTATUS, doPlayerUpdateValues);
			player.removeEventListener(df_optionsObject.SETOPTIONS, doSetOptionsPlayer);
		}
		
		protected function doPlayerUpdateValues(e: Event): void {
			updatePlayerValues();
		}
		
		protected function doSetOptionsPlayer(e: Event): void {
			updatePlayer();
		}
		
		protected function get player(): df_player {
			return dfApp.player;
		}
		
		protected function updatePlayerValues(): void {
			if (player.login) _login.text = player.login;
			_victory.value 	= player.options.fullVictory + player.options.victory;
			_lesions.value 	= player.options.fullLesions + player.options.lesions;
			_bonus.value 	= player.options.bonus;
		}
		
		protected function doLogin(e: Event): void {
			dispatchEvent(new Event(df_events.SHOW_LOGIN, true));
		}
		
		protected function updatePlayer(): void {
			_componentLayer.disposeChilds();
			if (player) {
				var inner: Rectangle = _componentLayer.getInner();
				var space: int = app.getStyleInnerSpace(style);
				if (player.login) {
					_login = new df_label(_componentLayer, inner.x, inner.y, inner.width, 22, style.login);
					inner.top += _login.height;
				}
				
				_victory = new df_paramItem(_componentLayer, locale.PARAM_VICTORY, player.options.fullVictory + player.options.victory, inner.x, inner.y, inner.width * 0.48, style.param1);
				_lesions = new df_paramItem(_componentLayer, locale.PARAM_LESIONS, player.options.fullLesions + player.options.lesions, inner.x + _victory.width, inner.y, inner.width * 0.52, style.param2);
				inner.top += _victory.height;
				_bonus = new df_paramItem(_componentLayer, locale.PARAM_BONUS, player.options.bonus, inner.x, inner.y, inner.width, style.param1);
				inner.top += _bonus.height;
				if (!player.source) {
					_loginButton = new uif_gbutton(_componentLayer, inner.x + (inner.width - se_util.DEFWIDTHBUTTON) / 2, inner.y,
						se_util.DEFWIDTHBUTTON, se_util.DEFHEIGHTBUTTON, 'button');
					
					_loginButton. text = locale.AUTORIZATION;
					_loginButton.addEvent(MouseEvent.CLICK, doLogin);
				}
				updatePlayerValues();
			}
		}
	}
}